package com.xpj.simulate;

import com.xpj.annotation.CacheEvict;
import com.xpj.annotation.CacheQuery;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

@Slf4j
@Service
public class ProductService {

    @CacheQuery(keyPrefix = "product:")
    public Product findProduct(String productId) {
        try {
            TimeUnit.SECONDS.sleep(1);
        } catch (InterruptedException e) {
            log.error("线程sleep异常", e);
        }
        return ProductDatabase.database.get(productId);
    }


    @CacheEvict(keyPrefix = "product:", keySpEL = "#product.id")
    public void update(Product product) {
        try {
            TimeUnit.SECONDS.sleep(1);
        } catch (InterruptedException e) {
            log.error("线程sleep异常", e);
        }
        ProductDatabase.database.put(product.getId(), product);
    }
}
